home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / PARSING.SWG / 0003_Command Line Parsing.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-17  |  5KB  |  157 lines

  1. ===========================================================================
  2.  BBS: Canada Remote Systems
  3. Date: 08-10-93 (01:00)             Number: 33744
  4. From: RYAN THOMPSON                Refer#: NONE
  5.   To: TERRY GRANT @ 912/701         Recvd: NO  
  6. Subj: RE: COMMAND LINE PARSING       Conf: (1221) F-PASCAL
  7. ---------------------------------------------------------------------------
  8. >>> Quoting message from Terry Grant @ 912/701 to All
  9. >>> Original sent 07 Aug 93  20:36:00 about Command Line Parsing
  10.  
  11. TG> Hello All!
  12. TG>
  13. TG>  After working on this for awhile, I thought mabe someone else could help
  14. TG> me out a little here. All I need this to do is Parse the command line for
  15. TG> seven parameters,
  16. TG>
  17. TG> The BaudRate     (/B),
  18. TG> :
  19. TG> and Overlay Size (/O).
  20. TG>
  21. TG>  My Main problem here is, it will SEE the command line, But WILL NOT allow
  22. TG> me to use anything AFTER the Switch ? Like /B2400 !
  23.  
  24.   Sure thing!  I once wrote a unit which among other things has some neat
  25. parsing for the command line.  Here's a snippet:
  26.  
  27. {- Top -}
  28.  
  29.   Function SwitchNum(S : String) : Integer;
  30.            { If a switch character specified exists, return which position }
  31.            { it is in on the command line.  Used internally. }
  32.     Var
  33.       Temp : String;
  34.       X,
  35.       Y : Integer;
  36.     Begin
  37.       Temp:= '';
  38.       X:= ParamCount;
  39.       Y:= 0;
  40.       while (X > 0) and (Y = 0) do begin
  41.         Temp:= ParamStr(X);
  42.         if (Temp[1] = '/') or (Temp[1] = '-') then
  43.           if UpCase(Temp[2]) = UpString(S) then Y:= X;
  44.         Dec(X);
  45.       end;
  46.       SwitchNum:= Y;
  47.     End;
  48.  
  49.  
  50.   Function SwitchThere(S : String) : Boolean;
  51.            { Returns TRUE if a switch of the character specified exists. }
  52.     Begin
  53.       If SwitchNum(S) = 0 then SwitchThere:= False
  54.       else SwitchThere:= True;
  55.     End;
  56.  
  57.  
  58.   Function SwitchData(S : String) : String;
  59.            { Return the data following a switch: /B2400 returns 2400. }
  60.     Var
  61.       Temp : String;
  62.     Begin
  63.       If SwitchNum(S) > 0 then begin
  64.         Temp:= ParamStr(SwitchNum(S));
  65.         Delete(Temp, 1, 2);
  66.       end
  67.       else Temp:= '';
  68.       SwitchData:= Temp;
  69.     End;
  70.  
  71.  
  72.   Function Parameter(N : Byte) : String;
  73.            { Returns the Nth command line parameter.  Parameters in quotes }
  74.            { are returned with the spaces in between:  /D Test "One Two" }
  75.            { Returns >Test< for Parameter(1) and >"One Two< for Parameter(2) }
  76.            { This allows you to, if you like, see what type of quote was }
  77.            { used, for perhaps literal vs. translate to ALL CAPS. }
  78.     Var
  79.       X,
  80.       Count : Byte;
  81.       Parm,
  82.       Temp : String;
  83.     Begin
  84.       X:= 0;
  85.       Count:= 0;
  86.       Parm:= '';
  87.       If ParamCount > 0 then repeat
  88.         Inc(X);
  89.         Temp:= ParamStr(X);
  90.         If (Temp[1] = '"') or (Temp[1] = '''') then begin
  91.           Parm:= Temp;
  92.           If X < ParamCount then repeat
  93.             Inc(X);
  94.             Parm:= Parm + ' ' + ParamStr(X);
  95.           until (Parm[Length(Parm)] = '"') or
  96.                 (Parm[Length(Parm)] = '''') or (X = ParamCount);
  97.           Inc(Count);
  98.         end
  99.         else if (Temp[1] <> '/') and (Temp[1] <> '-')
  100.         then begin
  101.           Inc(Count);
  102.           Parm:= Temp;
  103.         end;
  104.       until (X = ParamCount) or (Count = N);
  105.       If Count = N then Parameter:= Parm
  106.       else Parameter:= '';
  107.     End;
  108.  
  109.  
  110.   Function Parameters : Byte;
  111.            { Return the number of non-switch parameters on the command line. }
  112.     Var
  113.       X : Byte;
  114.     Begin
  115.       X:= 0;
  116.       If ParamCount > 0 then begin
  117.         Repeat
  118.           Inc(X)
  119.         Until Parameter(X) = '';
  120.         Parameters:= X - 1;
  121.       end
  122.       else Parameters:= 0;
  123.     End;
  124.  
  125. {- Fin -}
  126.  
  127.   A few examples:
  128.  
  129.   If SwitchThere('?') then DisplayHelp;
  130.   If SwitchThere('B') then BaudString:= SwitchData('B');
  131.   If Parameters < 1 then begin WriteLn('Too few parms'); Halt; end;
  132.   For X:= 1 to Parameters do
  133.   begin
  134.     Param[X]:= Parameter(X);
  135.   end;
  136.  
  137.   Sample command lines:
  138.  
  139.   TESTPROG /D /F TEST /B2400 "This is a test" /M-
  140.  
  141.   Parameters returns 2,
  142.   Parameter(1) returns TEST
  143.   Parameter(2) returns "This is a test
  144.   SwitchThere('L') returns False
  145.   SwitchData('M') returns -
  146.   SwitchData('G') returns null.
  147.  
  148.   I hope this helps you out!  It could be optimized a lot by simply reading
  149. all of the parameters into an array in your initialization code, to eliminate
  150. all of the redundant parsing, but I don't think that parsing time for a few
  151. hundred characters at most is a limiting factor of any sort.  ;-)
  152.  
  153. bye
  154. Ryan
  155.  
  156. --- Renegade v07-17 Beta
  157.